package javaTransactionAPI;

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

//bean用户自己管理，自己控制事务的开始，回滚，提交，结束
//container容器管理，默认选项
@Stateless
@Remote
@TransactionManagement(TransactionManagementType.BEAN)
public class StudentManagerBean2 implements StudentManager{
	
	@PersistenceContext
	private EntityManager em;
	
	@Resource
	private UserTransaction ut;
	
	@Override
	public void addStudent(Student student) {
		
		try {
			ut.begin();
			em.persist(student);
			ut.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				ut.rollback();
			} catch (IllegalStateException e1) {
				e1.printStackTrace();
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (SystemException e1) {
				e1.printStackTrace();
			}
		}
	}

}
